home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 19D (1987-07-22)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 19D (1987-07-22)(Pacific North-West Amigas Club)[WB].adf / PipeHandler1.2 / pipebuf.c < prev    next >
C/C++ Source or Header  |  1987-06-28  |  4KB  |  173 lines

  1. /****************************************************************************
  2. **  File:       pipebuf.c
  3. **  Program:    pipe-handler - an AmigaDOS handler for named pipes
  4. **  Version:    1.1
  5. **  Author:     Ed Puckett      qix@mit-oz
  6. **
  7. **  Copyright 1987 by EpAc Software.  All Rights Reserved.
  8. **
  9. **  History:    05-Jan-87       Original Version (1.0)
  10. */
  11.  
  12. #include   <libraries/dos.h>
  13. #include   <libraries/dosextens.h>
  14. #include   <libraries/filehandler.h>
  15. #include   <exec/exec.h>
  16.  
  17. #include   "pipelists.h"
  18. #include   "pipename.h"
  19. #include   "pipebuf.h"
  20. #include   "pipecreate.h"
  21. #include   "pipesched.h"
  22. #include   "pipe-handler.h"
  23.  
  24.  
  25.  
  26. /*---------------------------------------------------------------------------
  27. ** pipebuf.c
  28. ** ---------
  29. ** This module contains functions that manage the circular buffer for pipes.
  30. **
  31. ** Visible Functions
  32. ** -----------------
  33. **    PIPEBUF  *AllocPipebuf   (len)
  34. **    ULONG    MoveFromPipebuf (pb, dest, amt)
  35. **    ULONG    MoveToPipebuf   (pb, src, amt)
  36. **
  37. ** Macros (in pipebuf.h)
  38. ** ---------------------
  39. **    PipebufEmpty (pb)
  40. **    PipebufFull  (pb)
  41. **    FreePipebuf  (pb)
  42. **
  43. ** Local Functions
  44. ** ---------------
  45. **    - none -
  46. */
  47.  
  48.  
  49.  
  50. /*---------------------------------------------------------------------------
  51. ** AllocPipebuf() returns a pointer to a new PIPEBUF structure if there is
  52. ** enough free memory to allocate one with the requested ("len") storage.
  53. ** The structure is iinitialized as empty.  Notice that the buffer storage
  54. ** area is the tail part of the structure.
  55. */
  56.  
  57. PIPEBUF  *AllocPipebuf (len)
  58.  
  59. ULONG  len;
  60.  
  61. { PIPEBUF  *pb = NULL;
  62.  
  63.  
  64.   if ( (len > 0) && (len <= MAX_PIPELEN) &&
  65.        ((pb= (PIPEBUF *) AllocMem (sizeof (PIPEBUF) - 1 + len, ALLOCMEM_FLAGS)) != NULL) )
  66.     { pb->head= pb->tail= 0;
  67.       pb->full= FALSE;
  68.       pb->len= len;
  69.     }
  70.  
  71.   return pb;
  72. }
  73.  
  74.  
  75.  
  76. /*---------------------------------------------------------------------------
  77. ** Move bytes from the PIPEBUF to the memory pointed to by "dest".  At most
  78. ** "amt" bytes are moved.  The actual number moved is returned.
  79. */
  80.  
  81. ULONG  MoveFromPipebuf (pb, dest, amt)
  82.  
  83. PIPEBUF        *pb;
  84. register BYTE  *dest;
  85. ULONG          amt;
  86.  
  87. { register BYTE  *src;
  88.   register LONG  ct;
  89.   ULONG          amtleft;
  90.  
  91.  
  92.   if ((amt <= 0) || PipebufEmpty (pb))
  93.     return 0L;
  94.  
  95.   amtleft= amt;
  96.   src=  pb->buf + pb->tail;
  97.  
  98.   if (pb->tail >= pb->head)     /* then have to wrap around */
  99.     { if ((ct= (pb->len - pb->tail)) > amtleft)
  100.         ct= amtleft;     /* more than needed in end of pipebuf */
  101.  
  102.       CopyMem (src, dest, ct);
  103.       pb->tail= (pb->tail + ct) % pb->len;
  104.       amtleft -= ct;
  105.  
  106.       src= pb->buf + pb->tail;
  107.       dest += ct;
  108.     }
  109.  
  110.   if ( (amtleft > 0) && (ct= (pb->head - pb->tail)) )
  111.     { if (ct > amtleft)
  112.         ct= amtleft;     /* more than needed */
  113.  
  114.       CopyMem (src, dest, ct);
  115.       pb->tail += ct;     /* no need to mod */
  116.       amtleft  -= ct;
  117.     }
  118.  
  119.   pb->full= FALSE;     /* has to be: nonzero amt */
  120.  
  121.   return (amt - amtleft);
  122. }
  123.  
  124.  
  125.  
  126. /*---------------------------------------------------------------------------
  127. ** Move bytes to the PIPEBUF from the memory pointed to by "src".  At most
  128. ** "amt" bytes are moved.  The actual number moved is returned.
  129. */
  130.  
  131. ULONG  MoveToPipebuf (pb, src, amt)
  132.  
  133. PIPEBUF        *pb;
  134. register BYTE  *src;
  135. ULONG          amt;
  136.  
  137. { register BYTE  *dest;
  138.   register LONG  ct;
  139.   ULONG          amtleft;
  140.  
  141.  
  142.   if ((amt <= 0) || PipebufFull (pb))
  143.     return 0L;
  144.  
  145.   amtleft= amt;
  146.   dest= pb->buf + pb->head;
  147.  
  148.   if (pb->head >= pb->tail)     /* then have to wrap around */
  149.     { if ((ct= (pb->len - pb->head)) > amtleft)
  150.         ct= amtleft;     /* more than will fit in end of pipebuf */
  151.  
  152.       CopyMem (src, dest, ct);
  153.       pb->head= (pb->head + ct) % pb->len;
  154.       amtleft -= ct;
  155.  
  156.       src += ct;
  157.       dest= pb->buf + pb->head;
  158.     }
  159.  
  160.   if ( (amtleft > 0) && (ct= (pb->tail - pb->head)) )
  161.     { if (ct > amtleft)
  162.         ct= amtleft;     /* more than will fit */
  163.  
  164.       CopyMem (src, dest, ct);
  165.       pb->head += ct;     /* no need to mod */
  166.       amtleft -= ct;
  167.     }
  168.  
  169.   pb->full= (pb->head == pb->tail);
  170.  
  171.   return (amt - amtleft);
  172. }
  173.